import dayjs from 'dayjs'
import { Base64 } from 'js-base64'

/**
 * 新增千分位
 * @param {*} countNumber  源数据
 */
export function numberWithCommas(countNumber: number) {
  return countNumber.toString().replace(/\B(?=(\d{3})+(?!\d))/g, ',')
}

/**
 * 文件大小转换
 * @param size 文件大小
 * @param formUnit 原始单位
 * @param toUnit 转换后的单位
 * @param decimalPoint 保留小数
 * @param commas 是否转换千分位
 */
const unitList = ['B', 'KB', 'MiB', 'GiB', 'TB']
export function convertFileSize(
  size: number,
  formUnit = 'B',
  toUnitSource = '',
  decimalPoint = 2,
  commas = true
) {
  const fromIndex = unitList.indexOf(formUnit)
  let toUnit = toUnitSource
  if (!toUnitSource) {
    let toIndex = unitList.indexOf(formUnit)
    let calculate = size
    while (calculate > 1024) {
      calculate = Math.ceil(calculate / 1024)
      toIndex++
    }
    toUnit = unitList[toIndex]
  }
  const toIndex = unitList.indexOf(toUnit)
  let resultSize: number | string = size
  if (![fromIndex, toIndex].includes(-1)) {
    resultSize = size / Math.pow(1024, toIndex - fromIndex)
  }
  resultSize = Math.round(resultSize * Math.pow(10, decimalPoint)) / Math.pow(10, decimalPoint)
  if (commas) resultSize = numberWithCommas(resultSize)
  return toUnitSource ? resultSize : `${resultSize} ${toUnit}`
}

/**
 * 文件单位获取
 * @param size 文件大小
 * @param formUnit 原始单位
 */
export function getFileSizeUnit(size: number, formUnit = 'B') {
  let toIndex = unitList.indexOf(formUnit)
  let calculate = size
  while (calculate > 1024 && toIndex < unitList.length - 1) {
    calculate = Math.ceil(calculate / 1024)
    toIndex++
  }
  const toUnit = unitList[toIndex]
  return size !== 0 ? toUnit : 'KB'
}

export function getFileSizeMagnification(formUnit = 'B', toUnit = 'B') {
  const fromIndex = unitList.indexOf(formUnit)
  const toIndex = unitList.indexOf(toUnit)
  return Math.pow(1024, toIndex - fromIndex)
}

/**
 * 获取指定范围随机数
 * @param min 最小值
 * @param max 最大值
 */
export function getRandomNumber(min: number, max: number) {
  return Math.floor(Math.random() * (max - min + 1)) + min
}

/**
 * 获取指定范围随机数
 * @param start 开始时间戳
 * @param end 结束时间戳
 */
export function getDateDistance(start: number, end = dayjs().valueOf()) {
  const dateType = ['秒', '分钟', '小时', '天', '月', '年']
  const dateKey = ['second', 'minute', 'hour', 'day', 'month', 'year']
  const dateValve = [60, 60, 24, 30, 12, 9999]
  let distanceKey = 0
  let distanceValue = ''
  for (let i = 0; i < dateType.length; i++) {
    const item = dateType[i]
    const diffKey = dateKey[i]
    const max = dateValve[i]
    distanceKey = dayjs(end).diff(
      dayjs(start),
      diffKey as 'second' | 'minute' | 'hour' | 'day' | 'month' | 'year'
    )
    if (distanceKey < max) {
      distanceValue = `${distanceKey > 0 ? distanceKey : 0}${item}前`
      break
    }
  }
  return distanceValue
}

export function getBoxWidthFlexible(defaultWidth: number, elementName: string, margin = 20) {
  let boxWidth = defaultWidth
  const homeNode = document.getElementsByClassName(elementName)[0]
  const homeWidth = homeNode?.clientWidth || 0
  if (homeWidth) {
    const boxCount = Math.floor(homeWidth / (defaultWidth + margin))
    boxWidth = Math.floor((homeWidth - (boxCount + 2) * margin - 20) / boxCount)
  }
  return boxWidth
}

// 处理ws推过来的信息
export function handleWsMessage(data: string) {
  let msg = undefined
  try {
    const jsonData = JSON.parse(data)
    jsonData.data = Base64.decode(jsonData.data)
    if (jsonData.type !== 12) {
      msg = jsonData
    }
  } catch (error) {
    msg = undefined
  }
  return msg
}
